package com.ouyy.volunteerservice.volunteer.controller;
import com.ouyy.volunteerservice.volunteer.domain.po.Volunteer;
import com.ouyy.volunteerservice.volunteer.service.VolunteerService;
import com.ouyy.volunteerservice.volunteer.utils.EmailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import java.util.Map;

/**
 * @author ouyy
 * @date 2025/3/15 16:58
 * function: 忘记密码发送邮件功能
 */
@RestController
@RequestMapping("/api")
public class LossPasswordController {

    @Autowired
    private VolunteerService volunteerService;

    @PostMapping("/losspassWord")
    public String lossPassword(@RequestBody Map postbody) {
        try {
            String username = (String) postbody.get("username");
            String email = (String) postbody.get("email");
            String type = (String) postbody.get("type");

            if (type.equals("volunteer")) {
                Volunteer existingVolunteer = volunteerService.lambdaQuery()
                        .eq(Volunteer::getName, username)
                        .one();
                if (existingVolunteer != null) {
                    String password = existingVolunteer.getPassword();
                    EmailUtils.sendEmail(email, "尊敬的:" + username + " 以下是您的密码:", password);
                } else {
                    return "未找到该志愿者信息";
                }
            } else if (type.equals("organization")) {
                EmailUtils.sendEmail(email, "密码找回", "密码找回");
            }
            return "邮件已发送成功";

        } catch (MessagingException e) {
            e.printStackTrace();
            return "邮件发送失败: " + e.getMessage();
        } catch (Exception e) {
            e.printStackTrace();
            return "发生了一个错误: " + e.getMessage();
        }
    }



}
